package com.namecard.namecardapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.namecard.namecardapi.entity.TCate;
import com.namecard.namecardapi.entity.TCommodity;
import com.namecard.namecardapi.entity.vo.TCateQueryVo;
import com.namecard.namecardapi.mapper.TCateMapper;
import com.namecard.namecardapi.mapper.TCommodityMapper;
import com.namecard.namecardapi.service.TCateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cjl
 * @since 2024-04-21
 */
@Service
public class TCateServiceImpl extends ServiceImpl<TCateMapper, TCate> implements TCateService {

    @Resource
    private TCateMapper cateMapper;
    @Resource
    private TCommodityMapper commodityMapper;
    //分页查询
    @Override
    public IPage<TCate> selectPage(Page<TCate> pageParam, TCateQueryVo cateQueryVo) {
        IPage<TCate> pageModel = baseMapper.selectPage(pageParam, cateQueryVo);
        return pageModel;
    }

    //批量删除
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(List<Long> idList) {
        cateMapper.deleteBatchIds(idList);
        //同时删除商品的信息
        for (Long cateId : idList) {
            UpdateWrapper<TCommodity> wrapper = new UpdateWrapper<>();
            wrapper.set("cate_id",null).eq("cate_id",cateId);
            commodityMapper.update(null,wrapper);
        }

        return true;
    }
}
